package com.wad.manager.controller;

import com.github.pagehelper.PageInfo;
import com.wad.dto.system.SysRoleDto;
import com.wad.entity.system.SysRole;
import com.wad.manager.service.SysRoleService;
import com.wad.vo.common.Result;
import com.wad.vo.common.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author wad
 * @date 2023/12/01 10:56
 * @project e-commerce
 **/
@RequestMapping(value = "/admin/system/sysRole")
@RestController
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;


    /**
     * 查询角色列表
     * @param current 当前页
     * @param limit 每页显示的记录数
     * @param sysRoleDto 角色名称
     * @return
     */
    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable("current") Integer current,
                             @PathVariable("limit") Integer limit,
                             @RequestBody SysRoleDto sysRoleDto){
        PageInfo<SysRole> pageInfo=sysRoleService.findByPage(sysRoleDto,current,limit);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);

    }


    /**
     * 角色添加的方法
     * @param sysRole
     * @return
     */
    @PostMapping("/saveSysRole")
    public Result saveSysRole(@RequestBody SysRole sysRole){
        sysRoleService.saveSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    /**
     * 角色修改方法
     * @param sysRole
     * @return
     */
    @PutMapping("/updateSysRole")
    public Result updateSysRole(@RequestBody SysRole sysRole){
        sysRoleService.updateSysRole(sysRole);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }


    /**
     * 删除角色方法
     * @param roleId
     * @return
     */
    @DeleteMapping("/deleteById/{roleId}")
    public Result deleteById(@PathVariable("roleId") Long roleId){
        sysRoleService.deleteById(roleId);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有角色
     * @return
     */
    @GetMapping("/findAllRoles/{userId}")
    public Result findAllRoles(@PathVariable("userId") Long userId){
        Map<String,Object> map=sysRoleService.findAll(userId);
        return Result.build(map,ResultCodeEnum.SUCCESS);
    }
}
